package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.RedwineDetailResponse;
import com.redwine.app.net.responses.SendMsgEvent;
import com.redwine.app.utils.Utils;

import de.greenrobot.event.EventBus;


/**
 * 采购商认证主页
 * 作者：gassion on 17/8/27 13:49
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class PurchaserCertificationActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    public static final String PUBLISH_ENTITY = "publishEntity";
    private RedwineDetailResponse.DataEntity.PublishEntity publishEntity;
    @ViewInject(R.id.purchaser_certification_et_vendor_name)
    private EditText etVendorName;
    @ViewInject(R.id.purchaser_certification_et_license_number)
    private EditText etLicenseNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchaser_certification);
        ViewUtils.inject(this);
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (null != intent) {
            publishEntity = (RedwineDetailResponse.DataEntity.PublishEntity) intent.getSerializableExtra(PUBLISH_ENTITY);
        }
        headerTitle.setText(R.string.purchaser_certification);
        layoutBack.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.layout_back, R.id.purchaser_certification_tv_next,R.id.purchaser_certification_tv_instructions})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

            case R.id.purchaser_certification_tv_next:
                submit();
                break;

            case R.id.purchaser_certification_tv_instructions:
                Intent intent = new Intent(PurchaserCertificationActivity.this, CommonWebViewActivity.class);
                intent.putExtra(CommonWebViewActivity.TITLE_PARAMS, getString(R.string.purchaser_certification_agreement));
                String url = NetworkConstants.WEB_BUYER_URL;
                intent.putExtra(CommonWebViewActivity.LOAD_URL, url);
                startActivity(intent);
                break;
        }
    }

    private void submit() {
        String vendorName = etVendorName.getText().toString();
        if (TextUtils.isEmpty(vendorName)) {
            Utils.showToast("商家名称不能为空！");
            return;
        }
        String licenseName = etLicenseNumber.getText().toString();
        if (TextUtils.isEmpty(licenseName)) {
            Utils.showToast("营业执照号不能为空！");
            return;
        }

        publishEntity.setVendorName(vendorName);
        publishEntity.setLicenseNumber(licenseName);
        Intent intent = new Intent(PurchaserCertificationActivity.this, PurchaserCertificationLastActivity.class);
        intent.putExtra(ApplyForSupplierLastActivity.PUBLISH_ENTITY, publishEntity);
        startActivity(intent);
    }

    public void onEventMainThread(SendMsgEvent event) {
        if (event.getSendType() == SendMsgEvent.CLOSE_ACTIVITY) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
